/*
 * pps_bom
 */
package com.pitop.project.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.account.entity.Account;
import com.pitop.base.ctrl.BaseCtrl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.project.entity.Project;
import com.pitop.project.service.ProjectSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 项目 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/project")
@Slf4j
@Api(value = "项目控制器", description = "项目控制器")
public class ProjectCtrl extends BaseCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private ProjectSVImpl projectSVImpl;

    /**
     * 查询Project一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Project project = projectSVImpl.load(id, code);
        log.info(JSON.toJSONString(project));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }

    /**
     * 查询Project一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Project project = projectSVImpl.get(id, code);
        log.info(JSON.toJSONString(project));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }


    /**
     * 根据条件id查询Project一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Project project = projectSVImpl.loadById(id);
        log.info(JSON.toJSONString(project));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }

    /**
     * 根据条件code查询Project一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Project project = projectSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(project));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }

    /**
     * 根据条件id查询Project一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Project project = projectSVImpl.getById(id);
        log.info(JSON.toJSONString(project));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }

    /**
     * 根据条件code查询Project一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Project一个详情信息", notes = "查询Project一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public BeanRet getByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Project project = projectSVImpl.getByCode(code);
        log.info(JSON.toJSONString(project));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }

    /**
     * 分页查询Project信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "分页查询Project信息集合", notes = "分页查询Project信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "projectCategoryCode", value = "分类编码", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "自己级别", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "项目名", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目代码", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "项目说明", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态[开发Develop，稳定Stable，停用 Stop]", paramType = "query"),
            @ApiImplicitParam(name = "operateCode", value = "创建人编码", paramType = "query"),
            @ApiImplicitParam(name = "operateName", value = "创建人", paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Project project, @ApiIgnore Page<Project> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Project> projects = projectSVImpl.list(project, page.getCurPage(), page.getPageSize());
        int total = projectSVImpl.count(project);
        page.setTotalRow(total);
        page.setVoList(projects);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询Project信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Project信息集合", notes = "查询Project信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<Project> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Project> projects = projectSVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = projectSVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(projects);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建Project
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Project", notes = "创建Project")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectCategoryCode", value = "分类编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "项目名", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目代码", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "项目说明", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Project project, HttpServletRequest request) {
        if (StringUtils.isEmpty(project.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(project.getProjectCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (project.getStartTime() == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //获取登录用户
        Account account = this.loadAccount(request);
        project.setOperateCode(account.getCode());
        project.setOperateName(account.getAccount());
        projectSVImpl.save(project);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }

    /**
     * 修改Project
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Project", notes = "修改Project")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "项目名", paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目代码", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "项目说明", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query"),
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Project project) {
        if (project.getId() == null && StringUtils.isEmpty(project.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        projectSVImpl.modify(project);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, project);
    }

    /**
     * 修改项目状态
     *
     * @param code  组件编码
     * @param state 状态
     * @return
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1029)", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        if (StringUtils.isEmpty(code) || state == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        projectSVImpl.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 根据主键删除Project
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Project", notes = "删除Project")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        projectSVImpl.delete(id, code);
        return BeanRet.create(true, "删除Project成功");
    }

    /**
     * copy項目
     *
     * @return BeanRet
     */
    @ApiOperation(value = "copy項目", notes = "copy項目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping("/copy")
    @ResponseBody
    public BeanRet copy(String code, HttpServletRequest request) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //获取登录用户
        Account account = this.loadAccount(request);
        projectSVImpl.copy(code, account.getAccount(), account.getCode());
        return BeanRet.create(true, "copy項目成功");
    }

}
